package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.core.constants.HttpStatus;
import com.demo.core.domain.Result;
import com.demo.core.exception.ParameterException;
import com.demo.dao.CommentDao;
import com.demo.dao.LikeDao;
import com.demo.dao.ReplyDao;
import com.demo.domain.Like;
import com.demo.domain.ro.LikeRequest;
import com.demo.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;

@Service
public class LikeServiceImpl extends ServiceImpl<LikeDao, Like> implements LikeService {

    @Autowired
    LikeDao likeDao;

    @Autowired
    CommentDao commentDao;

    @Autowired
    ReplyDao replyDao;


    @Override
    public Result like(LikeRequest likeRequest,Long uid) throws IOException, ParameterException {
        String unickname = likeRequest.getUnickname();
        Long objId = likeRequest.getObjId();
        int objType = likeRequest.getObjType();
        if (likeRequest == null || unickname.isEmpty() || objId <= 0L) {
            throw new ParameterException("请求参数不正确");
        }

        Like like1 = likeDao.selectOne(new LambdaQueryWrapper<Like>()
                .eq(Like::getUnickname, unickname)
                .eq(Like::getObjId, objId)
                .eq(Like::getObjType, objType));

        if (like1 != null) {
            boolean flag = cancelLike(uid, objType, objId, like1);
            return flag ? Result.success(HttpStatus.SUCCESS, "取消点赞成功", null)
                    : Result.error("取消点赞失败");
        } else {
            boolean flag = like(uid, objType, objId,unickname);
            return flag ? Result.success(HttpStatus.SUCCESS, "点赞成功", null)
                    : Result.error("点赞失败");
        }
    }

    //    取消点赞
    @Override
    public boolean cancelLike(Long uid, int objType, Long objId, Like like1) throws ParameterException {
        if (like1 == null) {
            throw new ParameterException("like为空");
        }
        if (objType != 0 && objType != 1) {
            throw new ParameterException("objType不正确");
        }
        if (objId == 0L) {
            throw new ParameterException("objId为0");
        }
//           如果取消点赞的对象是评论，在评论表寻找对应的评论并减少点赞数
        if (objType == 0) {
            commentDao.decrementLikes(objId);
        }
//            如果取消点赞的对象是回复，在回复表寻找对应的回复并减少点赞数
        else if (objType == 1) {
            replyDao.decrementLikes(objId);
        } else {
            System.out.println("没有对应的类型");
        }
//            点赞表删除记录的条数
        int row = likeDao.deleteById(like1.getId());
        return row > 0;
    }

    //    点赞
    @Override
    public boolean like(Long uid, int objType, Long objId,String unickname) throws ParameterException {
        if (objType != 0 && objType != 1) {
            throw new ParameterException("传入的objType不正确");
        }
        if (objId == 0L) {
            throw new ParameterException("传入的objId为0");
        }
        Like like2 = new Like();
        like2.setUid(uid);
        like2.setUnickname(unickname);
        like2.setObjType(objType);
        like2.setObjId(objId);
        LocalDateTime localDateTime = LocalDateTime.now();
        like2.setCreateTime(localDateTime);
//        如果点赞的对象是评论
        if (objType == 0) {
            commentDao.incrementLikes(objId);
        }
//        如果点赞的对象是回复
        else if (objType == 1) {
            replyDao.incrementLikes(objId);
        } else {
            System.out.println("没有对应的类型");
        }
        return likeDao.insert(like2) > 0;
    }
}
